package com.ochnas.volunteersignup.service;

import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.ochnas.volunteersignup.domain.Event;
import com.ochnas.volunteersignup.domain.Position;
import com.ochnas.volunteersignup.domain.Time;
import com.ochnas.volunteersignup.domain.TimeSlot;

public class EventServiceImpl extends JpaDaoSupport implements EventService {

	@Override
	public void createEvent(Event e) {
		getJpaTemplate().persist(e);
	}

	@Override
	public Position createPositionForEvent(Event e, String name,
			String description, int requiredNumberOfVolunteers) {
		Position p = e.addPosition(name, description, requiredNumberOfVolunteers);
		e = getJpaTemplate().merge(e);
		getJpaTemplate().persist(e);
		return p;
	}

	@Override
	public TimeSlot createTimeSlotForPosition(Position p, Time startTime,
			Time endTime, int quantityOfVolunteers) {
		TimeSlot ts = p.addTimeSlot(startTime, endTime, quantityOfVolunteers);
		p = getJpaTemplate().merge(p);
		getJpaTemplate().persist(p);
		return ts;
	}

	@Override
	public void deleteEvent(Event e) {
		getJpaTemplate().remove(e);
	}

	@Override
	public void deletePositionOfEvent(Position p) {
		getJpaTemplate().remove(p);
	}

	@Override
	public void deleteTimeSlotOfPosition(TimeSlot ts) {
		getJpaTemplate().remove(ts);
	}

	@Override
	public void updateEvent(Event e) {
		e = getJpaTemplate().merge(e);
		getJpaTemplate().persist(e);
	}

	@Override
	public void updatePositionOfEvent(Position p) {
		p = getJpaTemplate().merge(p);
		getJpaTemplate().persist(p);
	}

	@Override
	public void updateTimeSlotOfPosition(TimeSlot ts) {
		ts = getJpaTemplate().merge(ts);
		getJpaTemplate().merge(ts);
	}

}
